//
//  MineViewController.m
//  Learn
//
//  Created by icomexe on 2018/10/27.
//  Copyright © 2018年 cjg. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "MineAPIManager.h"
#import "IFlyDetection.h"
#import "TextbookQuestionsModel.h"
#import "SubmitListenTestViewController.h"
@interface MineViewController ()

<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)NSString *nickName;

@property(nonatomic ,strong)NSString *avatar;

@property(nonatomic ,strong)NSString *schoolName;

@property(nonatomic ,strong)NSString *gradeName;

@property(nonatomic ,assign)BOOL isVip;



@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
   
    [MineAPIManager GetUserInformationWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
        NSDictionary *data = dic[@"data"];
        self.nickName = data[@"nickname"];
        self.avatar = data[@"avatar"];
        if(VALIDICTIONARY(data[@"school"]))self.schoolName = data[@"school"][@"name"];
        if(VALIDICTIONARY(data[@"grade"]))self.gradeName = data[@"grade"][@"gradeName"];
        lApp.schoolName = self.schoolName;
        lApp.gradeName = self.gradeName;
        if (data[@"vip_end_time"]&&[data[@"vip_end_time"] isKindOfClass:[NSString class]]) {
            NSString *str = data[@"vip_end_time"];
            if (str.length ) {
                self.isVip = YES;
            }
        }
        self.tableHeaderView = nil;
        [self getCustomInfo];
        [self.tableView setTableHeaderView:self.tableHeaderView];
    }];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
    [self.view addSubview:self.tableView];
    [self createLeftBarButtonItem];
    [self createRightBarButtonItem];
    
}
- (void)getCustomInfo
{
    [MineAPIManager GetMineSettingWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
        NSDictionary *data = dic[@"data"];
        
        lApp.mobileCustomer = data[@"mobile"];
        lApp.wechat_no = data[@"wechat_no"];
        lApp.wechat_img = data[@"wechat_img"];
        
    }];

}
- (void)createLeftBarButtonItem
{
    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 44)];
    UIImage *img = IMAGENAMED(@"ic_mine_title");
    [button setImage:img forState:UIControlStateNormal];
    [button setImage:img forState:UIControlStateHighlighted];
    button.width = img.size.width;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItems =@[item];
}
- (void)createRightBarButtonItem
{
    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 44)];
    UIImage *img = IMAGENAMED(@"ic_mine_set_white");
    [button setImage:img forState:UIControlStateNormal];
    [button setImage:img forState:UIControlStateHighlighted];
    button.width = img.size.width;
    [button addTarget:self action:@selector(goSet) forControlEvents:UIControlEventTouchUpInside];
   
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItems =@[item];
}
- (UIButton *)createButtonItemWithTitle:(NSString *)title img:(NSString *)img
{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, (lScreenWidth - 20)/3.00, 94);
        [button setTitle:title forState:UIControlStateNormal];
        [button setImage:IMAGENAMED(img) forState:UIControlStateNormal];
        button.titleLabel.font = MFontWithSize(16.f);
        [button setupEdgeInsetsByDirection:DFButtonImageDirectionBottom];
        [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    [button layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleTop imageTitleSpace:10];
  
    return  button;
   
   
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];
    
    if (!cell)
    {
        cell = [[MineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineTableViewCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    cell.data = self.titleArray[indexPath.row];
    if (!cell.switchBtn.hidden) {
        cell.switchBtn.on = lApp.is_eyeshield;
        [cell.switchBtn addTarget:self action:@selector(changeToProtectColor:) forControlEvents:UIControlEventValueChanged];
    }
   
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 52;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"清除缓存"]) {
        [self.parentViewController pushToViewControllerWithClassName:@"CleanCacheViewController" animated:YES];
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"投诉"]) {
        [self.parentViewController pushToViewControllerWithClassName:@"QuestionFeedBackVC" animated:YES];
    }
   
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"常见问题"]) {
        [self.parentViewController pushToViewControllerWithClassName:@"NormalQuestionsViewController" animated:YES];
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"联系客服"]) {
        [self.parentViewController pushToViewControllerWithClassName:@"ConnectCustomerViewController" animated:YES];
//        [self.parentViewController pushToViewControllerWithClassName:@"TestFinishViewController" animated:YES];
//        NSString *p = [[NSBundle mainBundle] pathForResource:@"1112" ofType:@"txt"];
//        NSData *d = [NSData dataWithContentsOfFile:p];
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingFragmentsAllowed error:nil];
//        TextbookQuestionsModel *model = [TextbookQuestionsModel mj_objectWithKeyValues:dic[@"json"]];
//        
//        UIViewController *vc = [[UIViewController alloc] initWithClassName:@"TestFinishViewController"];
//        vc.paraObject = dic;
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
        
//        SubmitListenTestViewController *vc = [[SubmitListenTestViewController alloc] init];
//        vc.model = model;
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
        
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"学习设置"]) {
        [self.parentViewController pushToViewControllerWithClassName:@"LearnSettingViewController" animated:YES];
    }
  
}
#pragma mark -event response
- (void)goSet
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"SetViewController"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}
- (void)changeToProtectColor:(UISwitch *)switchP
{
    lApp.is_eyeshield = switchP.on;
    USER_DEFAULTS_SAVE(ProtectColorSwitch, [NSNumber numberWithBool:lApp.is_eyeshield]);
    [lApp turnOrOffProtectColor];
   
}
- (void)tapClick
{
    [self.view endEditing:YES];
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, -lTopBarHeight, lScreenWidth, lScreenHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:rgba(245, 245, 245, 1)];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_tableView setTableHeaderView:self.tableHeaderView];
        [_tableView setTableFooterView:self.tableFootorView];
    }
    
    return _tableView;
}
- (UIImage *)imageWithBase64String:(NSString *)base64String {
   NSURL *URL = [NSURL URLWithString:base64String];
   NSData *imageData = [NSData dataWithContentsOfURL:URL];
   UIImage *image = [UIImage imageWithData:imageData];
   return image;
}
-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth,260/375.0*lScreenWidth + 72)];
        UIImageView *headerBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 260.0/375*lScreenWidth)];
        headerBgImageView.image = IMAGENAMED(@"ic_mine_header_bg");
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, lTopBarHeight + 28/375.00*lScreenWidth, 64, 64)];
        
        if (self.avatar.length > 0) {
            NSURL *url = [NSURL URLWithString:self.avatar];
            if ([self.avatar hasPrefix:@"http"]) {
                
                [headerImageView sd_setImageWithURL:url];
                
            }else{
              
                NSData*data = [NSData dataWithContentsOfURL:url];
                UIImage *img = [UIImage imageWithData:data];
                headerImageView.image = img;
                
            }
           
        }
        [headerImageView setAllCornersWithCornerRadius:32];
        [_tableHeaderView addSubview:headerBgImageView];
        [_tableHeaderView addSubview:headerImageView];
    
        UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, headerImageView.y + 6, lScreenWidth - 85 - 10, 25)];
        nameLabel.font = SFontWithSize(18);
        nameLabel.text = self.nickName.length > 0?self.nickName:@"";
        [nameLabel setTextAlignment:NSTextAlignmentLeft];
        nameLabel.textColor = lWhiteColor;
        
        UILabel * schoolLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, nameLabel.bottom + 10, lScreenWidth - 85 - 10, 16)];
        schoolLabel.font = RFontWithSize(14);
        self.schoolName = self.schoolName.length > 0?self.schoolName:@"";
        self.gradeName = self.gradeName.length > 0?self.gradeName:@"";
        schoolLabel.text = [NSString stringWithFormat:@"%@ %@",self.schoolName,self.gradeName];
        [schoolLabel setTextAlignment:NSTextAlignmentLeft];
        schoolLabel.textColor = rgba(196, 255, 230, 1);
        [_tableHeaderView addSubview:nameLabel];
        [_tableHeaderView addSubview:schoolLabel];
        
        UIView *cardView = [[UIView alloc] init];
        cardView.frame = CGRectMake(10,_tableHeaderView.height - 10 - 94,lScreenWidth - 20,94);
        cardView.backgroundColor = lWhiteColor;
        [cardView setAllCornersWithCornerRadius: 16];
        [_tableHeaderView addSubview:cardView];
        UIButton *vipBtn = [self createButtonItemWithTitle:@"会员中心" img:@"ic_mine_vip_center"];
        vipBtn.x = 0;
        [cardView addSubview:vipBtn];
        [vipBtn addAction:^(UIButton *btn) {
            [self.parentViewController pushToViewControllerWithClassName:@"VipCenterViewController" animated:YES];
        }];
  
        UIButton *sorceBtn = [self createButtonItemWithTitle:@"学习成绩" img:@"ic_mine_sorce"];
        sorceBtn.x = (lScreenWidth - 20)/3.00;
        [cardView addSubview:sorceBtn];
        [sorceBtn addAction:^(UIButton *btn) {
            
            [self.parentViewController pushToViewControllerWithClassName:@"SchoolRecordViewController" animated:YES];
        }];
        UIButton *personBtn = [self createButtonItemWithTitle:@"个人信息" img:@"ic_mine_person"];
        personBtn.x = (lScreenWidth - 20)*2/3.00;
        [cardView addSubview:personBtn];
        [_tableHeaderView setBackgroundColor:lClearColor];
        [personBtn addAction:^(UIButton *btn) {
            [self.parentViewController pushToViewControllerWithClassName:@"PersonInformationVC" animated:YES];
        }];
        
        UIImageView *vipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(schoolLabel.left, schoolLabel.bottom + 10, 60, 18)];
        vipImageView.image = IMAGENAMED(@"ic_mine_vip_grade");
        [_tableHeaderView addSubview:vipImageView];
        UILabel *vipLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 0, 42, 18)];
        vipLabel.font = SFontWithSize(12);
        vipLabel.text = @"VIP";
        [vipLabel setTextAlignment:NSTextAlignmentCenter];
        vipLabel.textColor = lWhiteColor;
        [vipImageView addSubview:vipLabel];
        vipImageView.hidden = !self.isVip;
        
    }
    
    return _tableHeaderView;
}

-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth - 20, 20)];
    }
    
    return _tableFootorView;
}
- (NSArray *)titleArray
{
//    @[@{@"title":@"学习设置",@"img":@"ic_mine_set_black"},@{@"title":@"护眼模式",@"img":@"ic_mine_eyes"},@{@"title":@"清除缓存",@"img":@"ic_mine_clean"},@{@"title":@"常见问题",@"img":@"ic_mine_question"},@{@"title":@"投诉",@"img":@"ic_mine_complain"},@{@"title":@"联系客服",@"img":@"ic_mine_phone"}];
    if (!_titleArray) {
        _titleArray = @[@{@"title":@"护眼模式",@"img":@"ic_mine_eyes"},@{@"title":@"清除缓存",@"img":@"ic_mine_clean"},@{@"title":@"常见问题",@"img":@"ic_mine_question"},@{@"title":@"投诉",@"img":@"ic_mine_complain"},@{@"title":@"联系客服",@"img":@"ic_mine_phone"}];
    }
    return _titleArray;
}
@end
